'''
This is a script that takes raw data and does spike detection and windows the spikes.
The final result is a cPickle of the windowed spikes.
'''

import numpy
import cPickle
from genutils.io import raw_data_columns
from sorting_functions import *

data_filename = 'data/test_data.cPickle'
threshold = 0.995

dt = 0.1 # in ms
archive_filename = 'spike_windows_' + str(int(threshold*1000)) + '.cPickle'

data = cPickle.load(open(data_filename))

spike_windows = {}
for set in data.keys():
    print 'starting processing data set '+ str(set)+ '...'
    spike_windows[set] = []
    for v in data[set]:
        print '    finding spikes...'
        crossings, peaks = find_spikes(v, dt, threshold)
        print '    windowing spikes...'
        sw, rejects = window_spikes(v,peaks)
        spike_windows[set].append( sw )
        
print 'archiving results to '+archive_filename
cPickle.dump(  spike_windows, open('data/'+archive_filename,'w'), protocol=-1  )

